﻿using System;
using System.Transactions;
using Ipmce.RemoteVoting.Backend.DataAccess.Exceptions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Ipmce.RemoteVoting.Backend.DataAccess.UnitTests
{
	[TestClass]
	public class VoterRepositoryTests
	{
		[TestInitialize]
		public void Init()
		{
			_transactionScope = new TransactionScope();
		}

		[TestCleanup]
		public void CleanUp()
		{
			_transactionScope.Dispose();
		}

		[TestMethod]
		public void TestGetAll()
		{
			using (var context = new RemoteVotingContext(ObjectMother.ConnectionString))
			{
				var repository = new VoterRepository(context);
				var result = repository.GetAllVoters();
				Assert.IsNotNull(result);
				Assert.IsTrue(result.Count > 0);
			} 
		}

		[TestMethod]
		public void TestCommitChanges()
		{
			const string newFio = "Гена Петров";
			string passportNumber;
			using (var context = new RemoteVotingContext(ObjectMother.ConnectionString))
			{
				var repository = new VoterRepository(context);
				var candidate = repository.GetAllVoters()[0];
				passportNumber = candidate.PassportNumber;
				candidate.Fio = newFio;
				repository.CommitChanges();
			}

			using (var context = new RemoteVotingContext(ObjectMother.ConnectionString))
			{
				var repository = new VoterRepository(context);
				var candidate = repository.GetByPassportNumber(passportNumber);
				Assert.AreEqual(newFio, candidate.Fio);
			}
		}

		[TestMethod]
		[ExpectedException(typeof(EntityNotFoundException))]
		public void TestNotFound()
		{
			using (var context = new RemoteVotingContext(ObjectMother.ConnectionString))
			{
				var repository = new VoterRepository(context);
				repository.GetByPassportNumber(Guid.NewGuid().ToString());
			}
		}

		private TransactionScope _transactionScope;
	}
}
